// pages/index/helpList/helpList.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId:0,
    pageNum:1,
    pageSize:10,
    height:wx.getSystemInfoSync().windowHeight
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(options){
    //调用接口获取求助列表
    this.setData({
      userId:parseInt(options.userId)
    })
    this.getData()
    
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onRefresh()
  },
  //刷新
  onRefresh(){
    //在当前页面显示导航条加载动画
    wx.showNavigationBarLoading(); 
    //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
    wx.showLoading({
      title: '刷新中...',
    })
    this.getData();
    console.log(this.data.userId)
  },
  //网络请求，获取数据
  getData(){
    var obj = this.data.userId==0?{pageNum:this.data.pageNum,pageSize:this.data.pageSize}:{pageNum:this.data.pageNum,pageSize:this.data.pageSize,userId:this.data.userId}
    console.log(obj)
    app.$http.get('/app/help/list',obj).then(res =>{
      this.setData({
        helpList:res.data.records
      })
      //隐藏loading 提示框
      wx.hideLoading();
      //隐藏导航条加载动画
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      wx.stopPullDownRefresh();
    }).catch(err => {
      console.log(err)
    })   
  },
  //滑动到底部
  lower(){
    this.appendData()
  },
  //网络请求，添加数据
  appendData(){
    var obj = this.data.userId==0?{pageNum:this.data.pageNum+1,pageSize:this.data.pageSize}:{pageNum:this.data.pageNum+1,pageSize:this.data.pageSize,userId:this.data.userId}
    app.$http.get('/app/help/list', obj).then(res =>{
      let list = this.data.helpList
      list.push.apply(list,res.data.records)
      this.setData({
        helpList:list,
        pageNum:this.data.pageNum+1
      })
    }).catch(err => {
      console.log(err)
    })   
  },
  //求助内容页面
  toHelp:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
        url: "/pages/index/help/help?id="+id
    })
  },
  //写求助
  toWriteHelp(){
    wx.navigateTo({
        url: "/pages/index/helpList/writeHelp/writeHelp"
    })
  }
})